﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;   //输入输出。

namespace 共享单车管理系统.star.Tgirbut
{
    public partial class Inster_prod : Form
    {
        public Inster_prod()
        {
            InitializeComponent();
        }

        public static string path_source="";


        SqlDataAdapter daProduct, dalog;
        DataSet ds=new DataSet();
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from Bike";
            string sdr = "select * from log";
            daProduct =new SqlDataAdapter(str,DB.cn); 
            dalog=new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds,"product_info");
            dalog.Fill(ds, "log_info");
        }

        void showAll()
        {
           DataView dvProduct=new DataView(ds.Tables["product_info"]);
            dgvProduet.DataSource = dvProduct;

        }

        

        private void Inster_prod_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“carDataSet15.BikeStatus”中。您可以根据需要移动或移除它。
            this.bikeStatusTableAdapter1.Fill(this.carDataSet15.BikeStatus);
            // TODO: 这行代码将数据加载到表“carDataSet14.BikeStatus”中。您可以根据需要移动或移除它。
            this.bikeStatusTableAdapter.Fill(this.carDataSet14.BikeStatus);


            init();
            showAll();

        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            if(textbikeid.Text=="")
            {
                MessageBox.Show("必填项不能为空");
            }
            else
            {
                DB.GetCn();
                string str = "select * from Bike where BikeID='" + textbikeid.Text + "'";

                DataTable dt =DB.GetDataSet(str);
                if(dt.Rows.Count>0)
                {
                    MessageBox.Show("单车编号已存在");
                }
                else
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_imges\\"
                        + txttepy.Text + "\\";

                    fileFolder += filename;

                    DataRow drpro = ds.Tables["product_info"].NewRow();
                    drpro["BikeID"] = Convert.ToInt32(textbikeid.Text);
                    drpro["Location"] = txtdizhi.Text;
                    drpro["Price"]= txtjiage.Text;
                   
                    drpro["kucun"]=txtkucun.Text;
                    drpro["miaoshu"] = textBox3.Text;
                    drpro["BikeType"] = txttepy.SelectedValue;
                    drpro["Status"]=txtzhuangtai.SelectedValue;

                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);
                      

                        drpro["img_dizhi"] = "\\Prod_imges\\" + txttepy.Text 
                            +"\\" + filename;
                     

                    }
                    else
                    {
                        drpro["img_dizhi"] = "无.gif";
                    }

                    ds.Tables["product_info"].Rows.Add(drpro);

                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["username"] = Login.UserID;
                    drLog["Type"] = "添加";
                    drLog["Time"] = DateTime.Now;
                    drLog["table"] = "product表";
                    ds.Tables["log_info"].Rows.Add(drLog);

                    try
                    {
                        SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                        daProduct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(dalog);
                        dalog.Update(ds, "log_info");
                        MessageBox.Show("添加成功");
                        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    }

                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();

                }
               
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonplc_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();   
        }
    }
}
